package com.hua.yuan.live.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.hua.yuan.common.BaseHolderView;
import com.hua.yuan.live.R;
import com.hua.yuan.live.view.holder_view.HolderView_Live;
import com.hua.yuan.live.listener.OnLiveClickListener;
import com.hua.yuan.live.repertory.cloud_data.res.RoomBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author richsjeson
 * 显示直播列表
 */
public class DisplayLiveAdapter extends BaseAdapter {


    private List<RoomBean> mList;

    private Context mContext;

    private  OnLiveClickListener mOnLiveClickListener;

    private static int mPosition;


    public  DisplayLiveAdapter(Context pContext,OnLiveClickListener pListener){

        this.mContext=pContext;
        this.mList=new ArrayList<>();
        this.mOnLiveClickListener=pListener;
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_live, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.i(getClass().getName(),"position:"+position);
        this.mPosition=position;
        holder.getView().bindHolder(mList,position,mContext);
        return convertView;
    }

    private class ViewHolder {


        private HolderView_Live mHolderView;

        public ViewHolder(View pView){

            mHolderView=new HolderView_Live(pView);
            mHolderView.setOnLiveClickListener(mOnLiveClickListener);
        }

        public BaseHolderView getView(){
            return mHolderView;
        }
    }

    public void setData(List<RoomBean> pList){

        if(pList==null){
            return;
        }

        if(pList.size()>0){
            mList.clear();
            mList.addAll(pList);
        }
        notifyDataSetChanged();
    }

    public List getData(){
        return mList;
    }
}
